<?php
class Detachievement_model extends MY_Model {
	
    function Detachievement_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "prestasi_pelanggaran_detil";
        $this->table_fields = array('detachiev_id','detachiev_nama','detachiev_tingkat','achiev_id');
    }
    
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
    	$db = $this->db;
    	$db->select('*');
		$db->join('prestasi_pelanggaran', 'prestasi_pelanggaran.achiev_id = '.$this->table_name.'.achiev_id');
		
		if ($search_key){
			if (!is_array($search_key)) $search_key = array($search_key);
			
			$qq = '';
			foreach($search_key as $kk){
				foreach($this->table_fields as $ff){
					$qq .= ($qq? " OR " : "").$this->table_name.".$ff LIKE ". $this->db->escape("%$kk%"); 
				}
				
				//join table
				foreach(array('achiev_jenis','achiev_bidang') as $ff){
					$qq .= ($qq? " OR " : "")."$ff LIKE ". $this->db->escape("%$kk%"); 
				}
			}
			$db->where("($qq)");
		}
		
		if ($where) $db->where($where);
		
    	if ($orderby) $db->order_by($orderby);
		
		$db->limit($limit, $offset);
		
		return $db->get($this->table_name);
    }
        
    function select_by_id($id, $orderby=false){
    	$db = $this->db;
    	$db->select('*');
		$db->join('prestasi_pelanggaran', 'prestasi_pelanggaran.achiev_id = '.$this->table_name.'.achiev_id');
    	if ($orderby) $db->order_by($orderby);
    	return $query = $this->db->get_where($this->table_name, array('detachiev_id' => $id));
    }
}